package clientUtilGUI;

import java.awt.Color;
import java.awt.GradientPaint;
import java.util.Locale;
import java.util.Vector;

import javax.swing.JScrollPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class ChartStatisticSalary extends JScrollPane {

	public static JFreeChart chart;
	
	public static Vector<GradientPaint> listGPSample(){
		Vector<GradientPaint> listGP = new Vector<GradientPaint>();
		// set up gradient paints for series...
        GradientPaint gp0 = new GradientPaint(
			        		0.0f, 0.0f, Color.blue,				// color bar
			                0.0f, 0.0f, new Color(0, 0, 64));	// Gradient
        GradientPaint gp1 = new GradientPaint(
			        		0.0f, 0.0f, Color.green,
			                0.0f, 0.0f, new Color(0, 64, 0));
        GradientPaint gp2 = new GradientPaint(
			        		0.0f, 0.0f, Color.red,
			                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp3 = new GradientPaint(
			        		0.0f, 0.0f, Color.pink,
			                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp4 = new GradientPaint(
			        		0.0f, 0.0f, Color.cyan,
			                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp5 = new GradientPaint(
			        		0.0f, 0.0f, Color.lightGray,
			                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp6 = new GradientPaint(
			        		0.0f, 0.0f, Color.magenta,
			                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp7 = new GradientPaint(
			        		0.0f, 0.0f, Color.orange,
			                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp8 = new GradientPaint(
			        		0.0f, 0.0f, Color.darkGray,
			                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp9 = new GradientPaint(
			        		0.0f, 0.0f, Color.yellow,
			                0.0f, 0.0f, new Color(64, 0, 0));
        GradientPaint gp10 = new GradientPaint(
			        		0.0f, 0.0f, Color.black,
			                0.0f, 0.0f, new Color(0, 0, 0));
        GradientPaint gp11 = new GradientPaint(
			        		0.0f, 0.0f, Color.white,
			                0.0f, 0.0f, new Color(255, 255, 255));
        listGP.add(gp0);
        listGP.add(gp1);
        listGP.add(gp2);
        listGP.add(gp3);
        listGP.add(gp4);
        listGP.add(gp5);
        listGP.add(gp6);
        listGP.add(gp7);
        listGP.add(gp8);
        listGP.add(gp9);
        listGP.add(gp10);
        listGP.add(gp11);
		return listGP;
	}
	
   /**
    * @author Tidus
    * @param ts: total salary
    * @param time: month/quarter/year
    * @param domainAxisLbl
    * @category : use for company
    */
    public ChartStatisticSalary(Vector<String> time, Vector<Integer> ts, String domainAxisLbl) {
        final CategoryDataset dataset = createDataset(time, ts);
        chart = createChart(dataset, domainAxisLbl);

        // add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        setViewportView(chartPanel);
    }
    
    /**
     * Creates a sample dataset.
     * @param ts 
     * @param time 
     * 
     * @return A dataset.
     */
    private CategoryDataset createDataset(Vector<String> time, Vector<Integer> ts) {
    	String series1 = "Lương";        

        // create the dataset...
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        for(int i=0; i<time.size(); i++){
        	dataset.addValue(ts.get(i), series1, time.get(i));
        	//value...row keys...column keys...
        }
        
        return dataset;
    }

    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * @param domainAxisLbl 
     * 
     * @return A chart.
     */
    private JFreeChart createChart(CategoryDataset dataset, String domainAxisLbl) {
        
        final JFreeChart chart = ChartFactory.createBarChart(
            "Tổng Lương Công Ty",  		// chart title
            domainAxisLbl,				// domain axis label
            "Tiền Lương (VNĐ)",			// range axis label
            dataset,                    // data
            PlotOrientation.VERTICAL, 	// orientation
            true,                       // include legend
            true, 						// tooltip
            false
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        // set the background color for the chart...
        chart.setBackgroundPaint(getBackground());

        // get a reference to the plot for further customisation...
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(false);
        plot.setRangeGridlinePaint(Color.white);

        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        
        // disable bar outlines...
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setMaximumBarWidth(0.04);
        renderer.setMinimumBarLength(0.02);
        renderer.setDrawBarOutline(false);
        
        // set color bar        
        for(int i =0; i<dataset.getColumnCount(); i++){
        	renderer.setSeriesPaint(i, listGPSample().get(i));
        };
         
        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//        rangeAxis.setRange(0.0, 100.0);
        rangeAxis.setAutoRange(true);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits(new Locale("vi")));
        
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        return chart;
        
    }
    
    
    /**
     * @author Tidus 
     * @param time month/quarter/year
     * @param ts: total salary (sort depId, time)
     * @param listDepName = list dep name [list series]
     * @param domainAxisLbl
     * @category : use for list dep
     */
    public ChartStatisticSalary(Vector<String> time, Vector<Integer> ts, Vector<String> listDepName, String domainAxisLbl) {
        final CategoryDataset dataset = createDataset_Dep(time, ts, listDepName);
        chart = createChart_Dep(dataset, domainAxisLbl);

        // add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        setViewportView(chartPanel);
    }
    
    private CategoryDataset createDataset_Dep(Vector<String> time, Vector<Integer> ts, Vector<String> listDepName) {     

        // create the dataset...
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        for(int i=0; i<time.size(); i++){
        	dataset.addValue(ts.get(i), listDepName.get(i), time.get(i));
        	//value...row keys...column keys...
        }
        
        return dataset;
	}
    
    private JFreeChart createChart_Dep(CategoryDataset dataset,	String domainAxisLbl) {

    	final JFreeChart chart = ChartFactory.createBarChart(
                "Tổng Lương",  				// chart title
                domainAxisLbl,				// domain axis label
                "Tiền Lương (VNĐ)",			// range axis label
                dataset,                    // data
                PlotOrientation.VERTICAL, 	// orientation
                true,                       // include legend
                true, 						// tooltip
                false
            );

            // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

            // set the background color for the chart...
            chart.setBackgroundPaint(getBackground());

            // get a reference to the plot for further customisation...
            CategoryPlot plot = chart.getCategoryPlot();
            plot.setBackgroundPaint(Color.lightGray);
            plot.setDomainGridlinePaint(Color.white);
            plot.setDomainGridlinesVisible(false);
            plot.setRangeGridlinePaint(Color.white);

            plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
            
            // disable bar outlines...
            BarRenderer renderer = (BarRenderer) plot.getRenderer();
            renderer.setMaximumBarWidth(0.04);
            renderer.setMinimumBarLength(0.02);
            renderer.setDrawBarOutline(false);
            
            // set color bar        
            for(int i =0; i<dataset.getColumnCount(); i++){
            	renderer.setSeriesPaint(i, listGPSample().get(i));
            };
             
            // change the auto tick unit selection to integer units only...
            final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//            rangeAxis.setRange(0.0, 100.0);
            rangeAxis.setAutoRange(true);
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits(new Locale("vi")));
            
            //	Category Rotation            
//            CategoryAxis domainAxis = plot.getDomainAxis();            
//			domainAxis.setCategoryLabelPositions(
//                    	CategoryLabelPositions.createUpRotationLabelPositions(
//                    					Math.PI / 6.0));
            // OPTIONAL CUSTOMISATION COMPLETED.
            
            return chart;
	}
	
	
    
    
    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************
    
//    /**
//     * Starting point for the demonstration application.
//     *
//     * @param args  ignored.
//     */
//    public static void main(String[] args) {
//
//        final ChartStatistic demo = new ChartStatistic();
//        
//        JFrame main = new JFrame("Chart");
//        main.setContentPane(demo);
//        main.setVisible(true);
//        main.pack();
//    }

}

